package com.juyu.miliao.api.controller;

import com.juyu.miliao.api.domain.dto.UserAuthenticationDTO;
import com.juyu.miliao.api.domain.vo.CheckUserAutenticationVO;
import com.juyu.miliao.api.service.UserAuthenticationInfoService;
import com.juyu.miliao.api.service.UserInfoService;
import com.juyu.miliao.api.service.VestRobotMessageService;
import com.juyu.miliao.common.domain.CommonHead;
import com.juyu.miliao.common.domain.Subject;
import com.juyu.miliao.common.exception.ConsumeRuntimeException;
import com.juyu.miliao.common.util.VerifyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 用户认证controller
 *
 * @author xuanqicheng
 * @create 2019-17-10:11
 */
@RestController
@RequestMapping("/userAuthentication")
public class UserAuthenticationInfoController {

    private String phoneRegex="^[0-9]{11}$";

    @Autowired
    private UserAuthenticationInfoService userAuthenticationInfoService;

    @Autowired
    private VestRobotMessageService vestRobotMessageService;


    /**
     * 检查用户认证信息
     */
    @GetMapping("/")
    public CheckUserAutenticationVO checkUserAutentication(Authentication token){
        Subject subject = (Subject) token.getPrincipal();
        CheckUserAutenticationVO checkUserAutenticationVO = userAuthenticationInfoService
                .checkUserAutenticationInfo(subject.getId());
        return checkUserAutenticationVO;
    }

    @GetMapping("/bindingPhone")
    public Object getPhone(Authentication token){
        Subject subject = (Subject) token.getPrincipal();

        Map<String,String> map = userAuthenticationInfoService.getPhone(subject.getId());
        return map;
    }


    /**
     * 发送验证码
     */
    @PostMapping("/code/phone")
    public void sendPhoneNumVerificationCode(HttpServletRequest request, Authentication token, String phone){
        if(phone == null || !phone.matches(phoneRegex)) {
            throw new ConsumeRuntimeException("手机号格式不正确");
        }
        CommonHead com = VerifyUtil.verifyCommonHead(request);
        Subject subject = (Subject) token.getPrincipal();
        //生成验证码
        String verificationCode =  (int)((Math.random()*9+1)*1000)+"";
        /**
         * 存入redis
         */
        userAuthenticationInfoService.sendPhoneNumValidateCode(subject.getId(),phone, verificationCode,com);
    }

    /**
     * 用户绑定手机号
     */

    @PostMapping("/bind/phone")
    public void getBindPhone(Authentication token, @Validated(UserAuthenticationDTO.BindingPhone.class) UserAuthenticationDTO
            userAuthenticationDTO, BindingResult bindingResult){


        if(bindingResult.hasErrors()){
            throw  new ConsumeRuntimeException(bindingResult.getFieldError().getDefaultMessage());
        }

        Subject subject = (Subject) token.getPrincipal();

        userAuthenticationDTO.setUserId(subject.getId());

        int userIsHost = subject.getIsHost();
        userAuthenticationInfoService.bindingPhone(userAuthenticationDTO,userIsHost);
    }

    @PostMapping("/bind/user")
    public void getBindUser(Authentication tk,@Validated(UserAuthenticationDTO.BindingUser.class) UserAuthenticationDTO
                            userAuthenticationDTO,BindingResult bindingResult){

        Subject subject = (Subject) tk.getPrincipal();

        if(bindingResult.hasErrors()) {
            throw  new ConsumeRuntimeException(bindingResult.getFieldError().getDefaultMessage());
        }

        if(userAuthenticationDTO.getAudioUrlTime() == null) {
            userAuthenticationDTO.setAudioUrlTime(String.valueOf(10 + (int)(Math.random()*30)));
        }

        userAuthenticationDTO.setUserId(subject.getId());

        userAuthenticationInfoService.bindingUser(userAuthenticationDTO);

        // 认证通过前主播收不到消息 故 去掉主播认证消息
//        vestRobotMessageService.getSendMsgByIos(subject.getId(),"您已提交认证申请，请耐心等待审核," +
//                "如有问题请联系官方客服ID：11241600");
    }
}
